package com.dj.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dj.dormitory.commonutils.Result;
import com.dj.dormitory.entity.RoleMenu;
import com.dj.dormitory.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DJ同学
 * @since 2021-01-31
 */
@RestController
@RequestMapping("/role-menu")
public class RoleMenuController {
    @Autowired
    private RoleMenuService roleMenuService;

    @RequestMapping("/edit/{type}/{ids}")
    public Result edit(@PathVariable(value = "type") String typeStr, @PathVariable(value = "ids") List<String> idsStrList){
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        Integer type = Integer.parseInt(typeStr);
        wrapper.eq("role_id",type);
        //先进行删除
        roleMenuService.remove(wrapper);
        //insert
        List<RoleMenu> roleMenus = new ArrayList<>();
        for(String idStr:idsStrList){
            roleMenus.add(new RoleMenu(type
                    ,Integer.parseInt(idStr)));
        }
        boolean save = roleMenuService.saveBatch(roleMenus);
        return save?Result.ok().message("编辑权限成功"):Result.error().message("编辑权限失败");
    }
}

